#!/bin/bash
# - benchmark

# We'll use the below global env vars, but not treat them as parameters to avoid
# duplicates in result path like /openeuler-20.03-aarch64/openeuler-aarch64-cifs-2003/
# os
# os_arch
# os_version
# os_mount

## makepkg is a script that automates the building of packages; it can download and validate source files,
## check dependencies, configure build-time settings, compile the sources, install into a temporary root,
## make customizations, generate meta-info, and package everything together.
## See: https://wiki.archlinux.org/index.php/Makepkg

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/misc-base.sh
. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/create_script_rootfs.sh

. $LKP_SRC/lib/job.sh
load_job_vars arch os os_arch os_mount os_version REMOTE_GIT_HOST REMOTE_GIT_PORT

[ -n "$benchmark" ]		|| die "benchmark is empty"
[ -n "$os_mount" ]		|| die "os_mount is empty"
[ -n "$os" ]			|| die "os is empty"
[ -n "$os_arch" ]		|| die "os_arch is empty"
[ -n "$os_version" ]		|| die "os_version is empty"

create_rootfs "cci-makepkg"

PKG_MNT=/initrd/pkg
mkdir -p "$PKG_MNT"

export DISTRO=${os}

[[ "$os_mount" = "cifs" ]] && os_mount="nfs"
pack_to=${os_mount}/${os}/${os_arch}/${os_version}/${benchmark}

. $LKP_SRC/lib/install.sh
. $LKP_SRC/distro/common
load_distro_funcs $DISTRO

cd $LKP_SRC/programs/$benchmark || cd $LKP_SRC/pkg/$benchmark || die "pkg is empty"

curl_pkgfile()
{
	curl -sS -H 'Content-Type: Application/json' -XPOST "$REMOTE_GIT_HOST:$REMOTE_GIT_PORT"'/git_command' \
	-d '{"git_repo": "'$1'", "git_command": ["git-show", "HEAD:'$2'"]}' -o "$2"
}

download_pkgfile()
{
	[ -n "$PKGBUILD_TAG" ] || return

	local pkg_tag_file="PKGBUILD-$PKGBUILD_TAG"
	local pkgbuild_dir="$LKP_SRC/programs/$benchmark/PKGBUILD"
	[ -f "$pkgbuild_dir" ] && return 
	curl_pkgfile "pkg/$benchmark" "$pkg_tag_file"

	[ -f "$pkg_tag_file" ] || die "$pkg_tag_file not exist"
	[ -s "$pkg_tag_file" ] || die "$pkg_tag_file is empty"
}

get_pkg_info()
{
	var=$1
	pkg_dir=${2:-.}
	(
		. $pkg_dir/PKGBUILD
		[ -n "$PKGBUILD_TAG" ] && . $pkg_dir/PKGBUILD-$PKGBUILD_TAG
		eval echo '$'$var
	)
}

# for packages with same dependencies, force the update instead of generating link
# due to schedule stage will auto detect symbolic link, so no need generate new link
check_shared_pkg()
{
	if [ -L $LKP_SRC/pkg/$benchmark ]; then
		basename $(readlink $LKP_SRC/pkg/$benchmark)
	else
		echo $benchmark
	fi
}

update_shared_pkg()
{
	local bm_link=$1

	[ "$bm_name" = "$benchmark" ] && return

	# benchmark is a symlink
	ln -sf latest.cgz "$sync_dest/${benchmark}.cgz" || return
	echo "update shared pkg link ${benchmark}.cgz -> ${bm_name}/${cgz_name}"
}

centos_update_gcc()
{
	grep -sqF "CentOS Linux release 7" /etc/centos-release || return

	[ -f /.dockerenv -o "$os_mount" = "initramfs" ] && {
		yum install -y centos-release-scl-rh
		yum install -y "devtoolset-8-gcc*"
	}
	source /opt/rh/devtoolset-8/enable
}

download_pkgfile

distro_install_depends lkp-dev

centos_update_gcc

distro_install_depends ${benchmark}-dev

[[ $arch ]] || arch=$(get_system_arch)

build_depends_pkg -i $benchmark $TMP

sync_dest="$PKG_MNT/$pack_to"
pkgver=$(get_pkg_info pkgver)
pkgrel=$(get_pkg_info pkgrel)
bm_name=$(check_shared_pkg)
cgz_name="${pkgver:-0}-${pkgrel:-0}.cgz"

setup_proxy

update_softlink()
{
	[ -e "$sync_dest/$cgz_name" ] || return

	ln -sf "$(basename $(realpath $sync_dest/$cgz_name))" "$sync_dest/latest.cgz" || return
	echo "create package: $sync_dest/latest.cgz -> $(realpath $sync_dest/$cgz_name)"

	update_shared_pkg "${bm_name}.cgz"

	[ -L $sync_dest/$cgz_name ] || return 0
	rm $sync_dest/$cgz_name
}

build_source_pkg()
{
	local mark="true"
	local pkg_args="-A --check --skippgpcheck"
	[ "${DISTRO}" == "archlinux" ] && {
		local mark="pacman"
		local pkg_args="${pkg_args} -s --needed --noconfirm"
	}
	local makepkg="$LKP_SRC/sbin/makepkg ${pkg_args} --config $LKP_SRC/etc/makepkg.conf"
	echo "${makepkg}"

	PACMAN="${mark}" BUILDDIR=$TMP CARCH=$arch PKGEXT=.cgz CGZDEST="$sync_dest/$cgz_name" PACKAGER="${PACKAGER:-Compass-CI}" $makepkg

	[ "$?" == 0 ] || exit 1
	update_softlink
	upload_to_target_dir ${sync_dest}
}

build_source_pkg
